package chp;

import java.util.Enumeration;

class SolutionChecker {
	
	/*
	 * This method determines if a given solution is ok for the given problem
	 * We will proceed this way:
	 * - take the data from the solution
	 * - try to rebuild the data from the problem
	 * - verify that the two arrays have the same content
	 * - if the k of the solution is bigger than the one given in the problem return NO already
	 */
	static int checkSolution(WICInput problem, WICOutput solution){
		int problemN = problem.getN();
		int problemK = problem.getK();
		int[] problemYSet = problem.getYSet(); //length n
		
		int solutionK = solution.getK();
		Enumeration<int[]> solutionYSet = solution.getData().elements();
		
		if(solutionK > problemK) //the solution has a bigger K than required
			return 1;
		
		int[] solutionCheck = new int[problemN];
		//populate the array with the default number 0
		for(int i=0; i<problemN; i++)
			solutionCheck[i] = 0;
		
		int[] temp = null;
		while(solutionYSet.hasMoreElements()){
			
			temp = solutionYSet.nextElement();
			
			for(int i=temp[0]-1; i<temp[1]-1; i++)
				solutionCheck[i] += temp[2];
		}
		
		if(compareArrays(solutionCheck, problemYSet))
			return 0;
		else
			return 2;		
	}
	

	private static boolean compareArrays(int[] a, int[] b){
		//we (safely) assume that the length is the same
		for(int i=0; i<a.length; i++)
			if(a[i] != b[i])
				return false;
		return true;		
	}
}
